﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Qiang.Data
{
    public class WorldFile
    {
        public WorldFile(string fileName)
        {
            this._fileName = GenerateFilename(fileName);
            if (File.Exists(fileName))
            {
                this.Open();
            }
        }



        private string _fileName = "";

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private double[] _affine;

        public double[] Affine
        {
            get { return _affine; }
            set { _affine = value; }
        }


        private double _cellWidth;

        public double CellWidth
        {
            get { return _cellWidth; }
            set { _cellWidth = value; }
        }

        private double _cellHeight;

        public double CellHeight
        {
            get { return _cellHeight; }
            set { _cellHeight = value; }
        }


        private double _topLeftX;

        public double TopLeftX
        {
            get { return _topLeftX; }
            set { _topLeftX = value; }
        }

        private double _topLeftY;

        public double TopLeftY
        {
            get { return _topLeftY; }
            set { _topLeftY = value; }
        }


        public void Open()
        {
            if (File.Exists(this._fileName) == false) return;
             StreamReader sr = new StreamReader(_fileName);
            this._affine = new double[6];
            this._affine[1] = NextValue(sr);  //x方向间距
            this._affine[2] = NextValue(sr);  //x方向倾斜,切变
            this._affine[4] = NextValue(sr);  //y方向倾斜，切变
            this._affine[5] = NextValue(sr);  //y方向间距
            this._affine[0] = NextValue(sr);  //左上角x坐标
            this._affine[3] = NextValue(sr);  //左上角y坐标
        }


        private static double NextValue(StreamReader sr)
        {
            string nextString = string.Empty;
            nextString = sr.ReadLine();
            while (nextString == string.Empty && sr.EndOfStream == false)
            {
                nextString = sr.ReadLine();
            }
            if (nextString != null)
            {
                return double.Parse(nextString, System.Globalization.CultureInfo.InvariantCulture);
            }
            else
            {
                return 0;
            }

        }


        public static string GenerateFilename(string imageFilename)
        {
            string ext = Path.GetExtension(imageFilename);
            string result = ".wld";
            switch (ext)
            {
                case ".bmp": result = ".bpw"; break;
                case ".emf": result = ".efw"; break;
                case ".exf": result = ".exw"; break;
                case ".gif": result = ".gfw"; break;
                case ".ico": result = ".iow"; break;
                case ".jpg": result = ".jpw"; break;
                case ".mbp": result = ".mww"; break;
                case ".png": result = ".pgw"; break;
                case ".tif": result = ".tfw"; break;
                case ".wmf": result = ".wft"; break;
            }
            return Path.ChangeExtension(imageFilename, result);
        }


    }
}
